<template>

  <div class="app-container">

    <h2 style="text-align: center;">修改轮播图</h2>

    <el-form label-width="120px">

        <el-form-item label="轮播图标题">
            <el-input v-model="bannerInfo.title" placeholder="简要的概述一下轮播图"/>
        </el-form-item>

        <el-form-item label="链接地址">
            <el-input v-model="bannerInfo.linkUrl"/>
        </el-form-item>

        <!-- 课程封面-->
        <el-form-item label="课程封面">
          <el-upload
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
            :action="BASE_API+'/ossservice/fileoss'"
            class="avatar-uploader">
            <img :src="bannerInfo.imageUrl" width="500px">
          </el-upload>
        </el-form-item>

        <el-form-item label="排序">
                <el-input-number v-model="bannerInfo.sort" controls-position="right" min="0"/>
            </el-form-item>

        <div class="el-form-item-my">
            <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
        </div>
    </el-form>
  </div>
</template>
<script>
import banner from '@/api/edu/banner'

export default {
  data() {
    return {
        bannerInfo: {
            id: '',
            title: '',
            imageUrl: 'https://jiyue-edu.oss-cn-shenzhen.aliyuncs.com/2020/06/02/7a46868a420741caa852dec36a2e8ff2u=225973054,2478979162&fm=26&gp=0.jpg',
            linkUrl: '',
            sort: 0,
        },
        BASE_API: process.env.BASE_API,// 接口API地址
        saveBtnDisabled: false // 保存按钮是否禁用
    }
  },

  created() {
    this.init()
  },
  watch: {// 监听事件
    $route(to, from) {// 路由变化方式，路由发生变化，方法就会执行
      this.init();
    }
  },

  methods: {

    // 初始化方法
    init() {
      if(this.$route.params && this.$route.params.id) {
        this.bannerInfo.id = this.$route.params.id;
        // 说明有参数，执行回显方法
        this.queryBannerById()
      } else {
        // 清空对象
        this.bannerInfo = {
            title: '',
            imageUrl: 'https://jiyue-edu.oss-cn-shenzhen.aliyuncs.com/2020/06/02/7a46868a420741caa852dec36a2e8ff2u=225973054,2478979162&fm=26&gp=0.jpg',
            linkUrl: '',
            sort: 0,
        }
      }
    },

    // 数据回显
    queryBannerById() {
      banner.queryBannerById(this.bannerInfo.id)
        .then(response => {
          this.bannerInfo = response.data.item
        })
    },

    // 新增Banner
    insertBanner() {
      banner.insertBanner(this.bannerInfo)
        .then(response => {
          this.$message({
            type: 'success',
            message: '添加Banner成功！'
          })
          // 跳转回banner列表
          this.$router.push({path: '/banner/table'})
        })
    },

    // 根据id更新Banner
    updateBannerById() {
      banner.updateBannerById(this.bannerInfo)
        .then(response => {
          this.$message({
            type: 'success',
            message: '更新Banner成功！'
          })
          // 跳转回banner列表
          this.$router.push({path: '/banner/table'})
        })
    },


    // 课程封面-上传成功
    handleAvatarSuccess(res, file) {
      console.log(res)// 上传相应
      console.log(URL.createObjectURL(file.raw))// base64编码
      this.bannerInfo.imageUrl = res.data.url
    },

    // 课程封面-上传成功后
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if(!isJPG) {
        this.$message.error('上传封面图片只能是 JPG 格式！')
      }
      if(!isLt2M) {
        this.$message.error('上传封面图片大小不能超过2MB')
      }
      return isJPG && isLt2M
    },

    

    // 保存并下一步
    saveOrUpdate() {
      
      // 判断是否更新
      if (this.$route.params && this.$route.params.id) {
        this.updateBannerById()
      } else {
        this.insertBanner()
      }
    },
    
  }
}
</script>

<style scoped>
.tinymce-container {
  line-height: 29px;
}
.el-form-item-my {
    width: 183px;
    margin: 0 auto;
}
</style>